package oa.hleast.xswl.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.pojo.domain.SysCommonConfig;
import oa.hleast.xswl.pojo.vo.SysCommonConfigVo;
import oa.hleast.xswl.service.ISysCommonConfigService;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 302
 * @description
 * @date 2021/3/29
 */
@Api(tags = "【系统管理】通用配置")
@RestController("SysCommonConfigController")
@RequestMapping("/api.admin/v1/commonConfig")
@AllArgsConstructor
@Slf4j
public class SysCommonConfigController {

    private final ISysCommonConfigService commonConfigService;

    @ApiOperation(value = "根据key获取系统配置-带缓存", httpMethod = "GET")
    @ApiImplicitParam(name = "configKey", value = "configKey", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)
    @GetMapping("getByConfigKey")
    public Result<SysCommonConfigVo> getByConfigKey(@RequestParam @NotBlank String configKey) {
        SysCommonConfig commonConfig = commonConfigService.getByConfigKey(configKey);
        SysCommonConfigVo sysCommonConfigVo = new SysCommonConfigVo();
        BeanUtil.copyProperties(commonConfig, sysCommonConfigVo);
        return Result.success(sysCommonConfigVo);
    }

    @ApiOperation(value = "新增通用配置", httpMethod = "POST")
    @ApiImplicitParam(name = "commonConfig", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysCommonConfig", dataTypeClass = SysCommonConfig.class)
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody SysCommonConfig commonConfig) {
        boolean status = commonConfigService.save(commonConfig);
        return Result.judge(status);
    }

    @ApiOperation(value = "修改通用配置byKey", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commonConfig", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysCommonConfig", dataTypeClass = SysCommonConfig.class)
    })
    @PutMapping(value = "updateByKey")
    public Result<Boolean> updateByKey(
            @RequestBody @NonNull SysCommonConfig commonConfig) {

        SysCommonConfig oldCommonConfig = commonConfigService.getByConfigKey(commonConfig.getConfigKey());

        if (ObjectUtil.isNull(oldCommonConfig)) {
            throw new BizException("数据不存在");
        }
        commonConfig.setId(oldCommonConfig.getId());
        boolean status = commonConfigService.updateById(commonConfig);

        return Result.judge(status);
    }

    @ApiOperation(value = "修改通用配置", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "commonConfig", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysCommonConfig", dataTypeClass = SysCommonConfig.class)
    })
    @PutMapping(value = "update/{id}")
    public Result<Boolean> update(
            @PathVariable @NonNull Integer id,
            @RequestBody @NonNull SysCommonConfig commonConfig) {

        if ((int) id != commonConfig.getId()) {
            throw new BizException("数据ID不一致");
        }
        boolean status = commonConfigService.updateById(commonConfig);

        return Result.judge(status);
    }

    @ApiOperation(value = "通用配置详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "String", dataTypeClass = String.class)
    @GetMapping("/{id}")
    public Result detail(@PathVariable String id) {
        System.out.println("11--------------------" + RequestUtils.getUserId());
        SysCommonConfig sysCommonConfig = commonConfigService.getById(id);
        return Result.success(sysCommonConfig);
    }

    @ApiOperation(value = "通用配置失效", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    @DeleteMapping("delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        SysCommonConfig commonConfigOld = commonConfigService.getById(id);

        commonConfigOld.setDeleted(true);
        boolean status = commonConfigService.updateById(commonConfigOld);

        return Result.judge(status);
    }

    @ApiOperation(value = "全部通用配置", httpMethod = "GET")
    @GetMapping("allList")
    public Result<List<SysCommonConfig>> list() {
        List<SysCommonConfig> list = commonConfigService.list();
        return Result.success(list);
    }
}
